<?php

/** 
 * 友盟用户模型
 * @property AndroidBroadcast $AndroidBroadcast
 * @property IOSBroadcast $IOSBroadcast
 * @property Umeng_log_mdl $umLog
 */
class Umeng_mdl extends CI_Model {
	const OS_ANDROID = 1;
	const OS_IOS     = 2;
	private $os      = 3;
	private $params   = array();
	public function __construct() {
		parent::__construct ();
		$this->load->model('Umeng_log_mdl','umLog');
	}
	
	private $cfg = array(
			'android'=>array(
					'appkey'=>'57c7dabcc62dca5655000125',
					'appMasterSecret'=>'on8nx4jrguzqwyjaemtjugv3zqxmovoe',
			),
			'ios'=>array(
					'appkey'=>'57c7e51ce0f55a799700266e',
					'appMasterSecret'=>'jz48m7kxtphjgoacaw1lmj1ukgyjnhxl',
			),
	);
	
	
	public function setOS($os = 'all'){
		switch ( strtolower($os) ) {
			case 'android':
				$this->os = self::OS_ANDROID;
				break;
			case 'ios':
				$this->os = self::OS_IOS;
				break;
			default : 
				$this->os = self::OS_ANDROID | self::OS_IOS;
				break;
		}
		return $this;
	}
	public function setParam($key,$value){
		$this->params[$key] = $value;
		return $this;
	}
	/**
	 * 向所有设备广播
	 * @abstract 默认向所有设备推送,如果要选择仅向IOS或ANDROID设备推送,请调用setOS();
	 * @param string $text		通知描述
	 * @param string $ticker	通知栏提示文字(仅ANDROID)
	 * @param string $title		通知标题(仅ANDROID)
	 * @param string $afterOpen	点击消息后的行为
	 * 		<p>"go_app"		: 打开应用(默认)
	 * 		<p>"go_url"		: 跳转到URL
	 * 		<p>"go_activity": 打开特定的activity
	 *		<p>"go_custom"	: 用户自定义内容。
	 * @param string $url		[可选]点击消息后跳转的URL,仅当$afterOpen=="go_url"时有效
	 * @return boolean
	 */
	public function broadcast($text,$ticker='',$title='',$afterOpen='go_app',$url=''){
		$flag   = 0 ;
		$result = array();
		try {
			if ( $this->os & self::OS_ANDROID ) {
				$this->load->library('umNotification/android/AndroidBroadcast',NULL,'AndroidBroadcast');
				$this->AndroidBroadcast->setAppMasterSecret($this->cfg['android']['appMasterSecret']);
				$this->AndroidBroadcast->setPredefinedKeyValue("appkey",           $this->cfg['android']['appkey']);
				$this->AndroidBroadcast->setPredefinedKeyValue("timestamp",        time());
				$this->AndroidBroadcast->setPredefinedKeyValue("ticker",           $ticker);
				$this->AndroidBroadcast->setPredefinedKeyValue("title",            $title);
				$this->AndroidBroadcast->setPredefinedKeyValue("text",             $text);
				$this->AndroidBroadcast->setPredefinedKeyValue("after_open",       $afterOpen);
				$this->AndroidBroadcast->setPredefinedKeyValue("production_mode", "true");
				if ( ! empty( $url ) && $afterOpen=='go_url'  ) {
					$this->AndroidBroadcast->setPredefinedKeyValue('url', $url);
				}
				$result['android'] = $this->AndroidBroadcast->send();
				if( $result['android'] ) $flag = $flag | self::OS_ANDROID;
			}
			
			if ( $this->os & self::OS_IOS ) {
				$this->load->library('umNotification/ios/IOSBroadcast',NULL,'IOSBroadcast');
				$this->IOSBroadcast->setAppMasterSecret($this->cfg['ios']['appMasterSecret']);
				$this->IOSBroadcast->setPredefinedKeyValue("appkey",           $this->cfg['ios']['appkey']);
				$this->IOSBroadcast->setPredefinedKeyValue("timestamp",        time());
				$this->IOSBroadcast->setPredefinedKeyValue("alert", $text);
				$this->IOSBroadcast->setPredefinedKeyValue("production_mode", "true");
				if ( ! empty( $url ) && $afterOpen=='go_url' ) {
					$this->IOSBroadcast->setCustomizedField('rj_after_open','go_url');
					$this->IOSBroadcast->setCustomizedField('rj_url',$url);
				}
				$result['ios'] = $this->IOSBroadcast->send();
				if( $result['ios'] ) $flag = $flag | self::OS_IOS;
			}
			
			$log = array();
			$log['os']   = $this->os;
			$log['type'] = 'broadcast';
			$log['text'] = $text;
			$log['ticker'] = $ticker;
			$log['title']  = $title;
			$log['afterOpen'] = $afterOpen;
			$log['url'] = $url;
			$log['result'] = json_encode($result);
			$this->umLog->insert($log);
			
			return $this->os === $flag;
		} catch (Exception $e) {
			pr($e);
			die;
			return FALSE;
		}
	}
}

